package com.gzxxgl.user;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gzxxgl.enu.EnumMsg;
import com.gzxxgl.enu.TableSequence;
import com.gzxxgl.model.data.UserOrganiz;
import com.gzxxgl.util.QTree;
import com.jfinal.core.Controller;
public class UserOrganizController extends Controller {


	public void findOrganizsTree() {

		setAttr("dlsjjl", "查询组织关系");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", false);
		m.put("msg", EnumMsg.ERR);
		BigDecimal s = new BigDecimal(-1);
		QTree q = new QTree(s, "", "同兴智能科技有限公司",null,"g_user_organiz", null);
		renderJson(q);

	}

	public void add(){

		setAttr("dlsjjl", "新增组织关系");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", false);
		m.put("msg", EnumMsg.ERR);
		UserOrganiz u0 =  UserOrganiz.dao.findById(getPara("id"));
		String le = "";
		switch (u0.getLevl()){
		case "A" :
		 le = "B";
		break;
		case "B" :
			 le = "C";
		break;
		default : 
		 le = "C";
		break;
		
		}
		UserOrganiz u = new UserOrganiz();
		boolean flag = u.set("organizid", TableSequence.organiz_id_sequence.name() + TableSequence.val)
				.set("oname", getPara("name").trim())
				.set("pid", getPara("id"))
				.set("levl", le)
				.set("bmbh", getPara("bm"))
				.save();
		if (flag) {
			m.put("flag", true);
			m.put("msg", EnumMsg.SUCCESS);
		}
		renderJson(m);
	}
	
	public void findbylevl(){
		setAttr("dlsjjl", "查询组织关系");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", false);
		m.put("msg", EnumMsg.ERR);
		List<UserOrganiz> l = UserOrganiz.dao.querylevl(getPara("level"));
		if(l.size()>0){
			m.put("flag", true);
			m.put("msg", EnumMsg.SUCCESS);
			m.put("list", l);
		}
		renderJson(m);
	}
	public void findbypid(){

		setAttr("dlsjjl", "查询下级部门");
		Map<String, Object> m = new HashMap<>();
		m.put("flag", false);
		m.put("msg", EnumMsg.ERR);
		UserOrganiz oz = UserOrganiz.dao.queryOrg(getPara("pid"));
		if(oz != null){
		List<UserOrganiz> l = UserOrganiz.dao.querypid(oz.get("organizid"));
		if(l.size()>0){
			m.put("flag", true);
			m.put("msg", EnumMsg.SUCCESS);
			m.put("list", l);
		}
		}
		renderJson(m);
		
	}
}